﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WLYT.Application.Interfaces;
using WLYT.Domain.ApiModel;
using WLYT.Domain.Enum;

namespace WLYT.Admin.WebApi.Controllers
{
    [AllowAnonymous]
    public class AgvController : ApiController
    {
        private readonly IAgvService AgvService;
        private readonly IOrderService OrderService;
        public AgvController(IAgvService agvService, IOrderService orderService)
        {
            AgvService = agvService;
            OrderService = orderService;
        }

        /// <summary>
        /// 具体点位 =》 具体点位
        /// </summary>
        /// <param name="from">起点</param>
        /// <param name="to">终点</param>
        /// <returns></returns>
        [Route("api/agv/Run")]
        public Task<ApiResult> Run(string from, string to)
        {
            return AgvService.Run(from, to);
        }

        /// <summary>
        /// 具体点位 =》 某个区域
        /// </summary>
        /// <param name="from">位置</param>
        /// <param name="to">前往点位</param>
        /// <returns></returns>
        [Route("api/agv/Go")]
        public Task<ApiResult> Go(string from, AgvPointMapType to)
        {
            return AgvService.Run(from, to);
        }

        /// <summary>
        /// 收料区 =》 全自动贴标机
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/ToAutoLabel")]
        public Task<ApiResult> ToAutoLabel()
        {
            return AgvService.Run(AgvPointMapType.收料区, AgvPointMapType.全自动贴标机, AgvPodType.工装_7寸);
        }

        /// <summary>
        /// 收料区 =》 湿敏仓
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/ToSMC")]
        public Task<ApiResult> ToSMC()
        {
            return AgvService.Run(AgvPointMapType.收料区, AgvPointMapType.湿敏仓_出库, AgvPodType.料箱);
        }

        /// <summary>
        /// 收料区 =》 点料机_入料口
        /// </summary>
        /// <param name="from">点位</param>
        /// <returns></returns>
        [Route("api/agv/ToDLJ")]
        public async Task<ApiResult> ToDLJ(string from)
        {
            ApiResult result = await AgvService.Run(from, AgvPointMapType.点料机_入料口);
            return result;
        }

        /// <summary>
        /// 收料区 =》 （全自动贴标机，湿敏仓，点料机_入料口）
        /// </summary>
        /// <param name="from">位置</param>
        /// <param name="isReturn">是否退料</param>
        /// <returns></returns>
        [Route("api/agv/To")]
        public Task<ApiResult> To(string from, bool isReturn = false)
        {
            return AgvService.To(from, isReturn);
        }

        /// <summary>
        /// 点料机_出料口 =》 全自动贴标机
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/DLJToAutoLabel")]
        public Task<ApiResult> DLJToAutoLabel()
        {
            return AgvService.Run(AgvPointMapType.点料机_出料口, AgvPointMapType.全自动贴标机, AgvPodType.工装_7寸);
        }

        /// <summary>
        /// 点料机_出料口 =》 湿敏仓
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/DLJToHalfAutoLabel")]
        public Task<ApiResult> DLJToHalfAutoLabel()
        {
            return AgvService.Run(AgvPointMapType.点料机_出料口, AgvPointMapType.湿敏仓_入库, AgvPodType.工装_13寸);
        }

        /// <summary>
        /// 湿敏仓 =》 发料区
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/SMC_Out")]
        public Task<ApiResult> SMC_Out()
        {
            return AgvService.Run(AgvPointMapType.湿敏仓_出库, AgvPointMapType.发料区, AgvPodType.料箱, 1);
        }

        /// <summary>
        /// 智能货柜 =》 发料区
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/SMT_Out")]
        public Task<ApiResult> SMT_Out()
        {
            return AgvService.Run(AgvPointMapType.智能货柜, AgvPointMapType.发料区, AgvPodType.料箱, 1);
        }

        /// <summary>
        /// 临时点位 =》 点料机_出料口
        /// </summary>
        /// <param name="podType">工装类型（0：7寸:1：13寸）</param>
        /// <returns></returns>
        [Route("api/agv/ToDLJOut")]
        public Task<ApiResult> ToDLJOut(AgvPodType podType)
        {
            return AgvService.ToUse(AgvPointMapType.点料机_出料口, podType);
        }

        /// <summary>
        /// 退料区 =》 收料区
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/Return")]
        public Task<ApiResult> Return()
        {
            AgvService.Run(AgvPointMapType.临时, AgvPointMapType.退料区, AgvPodType.料箱);
            return AgvService.Run(AgvPointMapType.退料区, AgvPointMapType.收料区, AgvPodType.料箱);
        }

        /// <summary>
        /// 空箱 =》 退料区
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/ToReturn")]
        public Task<ApiResult> ToReturn()
        {
            return AgvService.ToUse(AgvPointMapType.退料区, AgvPodType.料箱);
        }

        /// <summary>
        /// 全自动贴标机 =》 临时点位
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/AutoLabelToUnUse")]
        public async Task<ApiResult> AutoLabelToUnUse()
        {
            var result = await AgvService.ToUnUse(AgvPointMapType.全自动贴标机);
            if(result.Code != 0)
            {
                result = await AgvService.Run(AgvPointMapType.全自动贴标机, AgvPointMapType.收料区);
            }
            return result;
        }

        /// <summary>
        /// 点料机_入料口 =》 临时点位
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/DLJ_InToUnUse")]
        public async Task<ApiResult> DLJ_InToUnUse()
        {
            var result = await AgvService.ToUnUse(AgvPointMapType.点料机_入料口);
            if (result.Code != 0)
            {
                result = await AgvService.Run(AgvPointMapType.点料机_入料口, AgvPointMapType.收料区);
            }
            return result;
        }

        /// <summary>
        /// 湿敏仓 =》 临时点位
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/SMCToUnUse")]
        public Task<ApiResult> SMCToUnUse()
        {
            return AgvService.ToUnUse(AgvPointMapType.湿敏仓_入库);
        }

        /// <summary>
        /// 获取AGV任务状态
        /// </summary>
        /// <param name="code">任务号</param>
        /// <returns></returns>
        [Route("api/agv/GetAgvStatus")]
        public Task<ApiResult> GetTaskStatus(string code)
        {
            return AgvService.GetTaskStatus(code);
        }

        /// <summary>
        /// 获取点料机入料口 AGV任务状态
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/GetDLJInStatus")]
        public Task<ApiResult> GetDLJInStatus()
        {
            return AgvService.GetTaskStatus(AgvPointMapType.点料机_入料口);
        }

        /// <summary>
        /// 获取点料机出料口 AGV任务状态
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/GetDLJOutStatus")]
        public Task<ApiResult> GetDLJOutStatus()
        {
            return AgvService.GetTaskStatus(AgvPointMapType.点料机_出料口);
        }

        /// <summary>
        /// 获取全自动贴标机 AGV任务状态
        /// </summary>
        /// <returns></returns>
        [Route("api/agv/GetAutoLabelStatus")]
        public Task<ApiResult> GetAutoLabelStatus()
        {
            return AgvService.GetTaskStatus(AgvPointMapType.全自动贴标机);
        }

        /// <summary>
        /// 海康Agv回调
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/agv/agvCallbackService/agvCallback")]
        public async Task<ResultModel> AgvCallback([FromBody] AgvRequest request)
        {
            if (request.method == "outbin")
            {
                await AgvService.AgvOutBin(request.taskCode);
            }
            else if (request.method == "end")
            {
                await AgvService.AgvCompleted(request.taskCode);
            }

            var model = new ResultModel
            {
                code = "0",
                message = "成功",
                reqCode = request.reqCode
            };
            return model;
        }

        /// <summary>
        /// 继续任务(发料区)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/agv/ContinueTask")]
        public Task<ApiResult> ContinueTask()
        {
            return AgvService.ContinueTask("A11");
        }

        public class ResultModel
        {
            public string code { get; set; }
            public string message { get; set; }
            public string reqCode { get; set; }
        }
    }
}
